package com.google.code.mochaccino.framework.transaction.provider;

/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import bitronix.tm.resource.jdbc.PoolingDataSource;
import com.google.code.mochaccino.framework.transaction.jsr250.BitronixDataSourceWrapper;
import com.google.common.base.Preconditions;
import java.util.Properties;
import javax.sql.DataSource;
import javax.sql.XADataSource;

/** A XADataSource Provider Implementation for a Standard DataSource. */
public final class XADataSourceProvider extends XAResourceProvider<PoolingDataSource, DataSource> {

	private Class<? extends XADataSource> dataSourceClass      = null;
	private Properties                    dataSourceProperties = null;

	public XADataSourceProvider(
			String uniqueName, Class<? extends XADataSource> dsClass, Properties dataSourceProperties ) {
		super( Preconditions.checkNotNull( uniqueName ) );
		this.dataSourceClass = Preconditions.checkNotNull( dsClass );
		this.dataSourceProperties = Preconditions.checkNotNull( dataSourceProperties );
	}

	@Override
	public DataSource get() {
		PoolingDataSource bean = super.createConfiguredResource();
		bean.setClassName( dataSourceClass.getName() );
		bean.setDriverProperties( dataSourceProperties );
		return new BitronixDataSourceWrapper( bean );
	}

	@Override
	protected PoolingDataSource createUnconfiguredResource() {
		return new PoolingDataSource();
	}
}
